<template>
  <div>
    <h1>存钱宝</h1>
  </div>

  <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="total" label="总资产" width="180" />
    <el-table-column prop="yield" label="预计收益" />
  </el-table>
  <div>
    <div>
      <div class="mb-4">
        <el-button type="info" @click="openDialogTwo()">提现</el-button>
        <el-button type="warning" @click="openDialog()">充值</el-button>
      </div>
    </div>
  </div>
  <div>
    <el-dialog v-model="dialogFormVisible" title="充值" width="500">
      <el-form :model="form">
        <el-form-item label="充值金额" :label-width="formLabelWidth">
          <el-input v-model="form.shoppingMoeny" autocomplete="off" />
        </el-form-item>
        <el-form-item label="选择银行卡" :label-width="formLabelWidth">
          <el-select v-model="form.bank" placeholder="请选择银行卡">
            <el-option label="农村商业银行" value="农村商业银行" />
            <el-option label="工商银行" value="工商银行" />
            <el-option label="建设银行" value="建设银行" />
            <el-option label="招商银行" value="招商银行" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="tixian(2)">提交</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
  <div>
    <el-dialog v-model="dialogFormVisibleTwo" title="提现" width="500">
      <el-form :model="form">
        <el-form-item label="提现金额" :label-width="formLabelWidth">
          <el-input v-model="form.shoppingMoeny" autocomplete="off" />
        </el-form-item>
        <el-form-item label="选择银行卡" :label-width="formLabelWidth">
          <el-select v-model="form.bank" placeholder="请选择银行卡">
            <el-option label="农村商业银行" value="农村商业银行" />
            <el-option label="工商银行" value="工商银行" />
            <el-option label="建设银行" value="建设银行" />
            <el-option label="招商银行" value="招商银行" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="tixian(1)">提交</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
const tableData = ref([]);
import { reactive, ref, onMounted } from "vue";
import { assetList, getBrand } from "../api/api";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
const dialogFormVisible = ref(false);
const dialogFormVisibleTwo = ref(false);
const formLabelWidth = "140px";
const asslist = ref({});
const router = useRouter();
let userId = ref("");
const listHH = () => {
  console.log("当前userId" + userId);
  assetList(userId).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data;
      asslist.value = res.data.data.total;
      sessionStorage.setItem("asset", JSON.stringify(assetList));
    }
  });
};

const tixian = (any: any) => {
  form.value.statusId = any;
  console.log(form.value);
  getBrand(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success(res.data.msg);
      dialogFormVisible.value = false;
      listHH();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

const openDialogTwo = () => {
  dialogFormVisibleTwo.value = true;
};

const openDialog = () => {
  dialogFormVisible.value = true;
  //   let a = sessionStorage.getItem("asset");
  //   //   console.log(u);
  //   if (a != null) {
  //     let asset = JSON.parse(a);
  //     console.log("总资产" + asset);
  //     form.name = asset;
  //   }
};

const form = ref({
  balance: null,
  bank: "",
  shoppingMoeny: null,
  statusId: null,
  userId: null,
});
// 页面加载钩子函数
onMounted(() => {
  let u = sessionStorage.getItem("user");
  //   console.log(u);
  if (u != null) {
    let user = JSON.parse(u);
    console.log(user);
    userId = user.id;
    form.value.userId = user.id;
    console.log("当前userId" + userId);
  }

  listHH();
});
</script>
